package me.kuku.test;

import cn.u313.model.MsgInfo;
import lombok.Getter;
import lombok.Setter;
import me.kuku.utils.OkHttpUtils;
import okhttp3.Response;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import static org.apache.commons.lang3.CharEncoding.UTF_8;

/**
 * 支付宝打卡
 */
public class SignIn {
    // TODO: 2021/12/25 学号
    private final static String ID = "20196265";
    // TODO: 2021/12/25 省
    private final static String PROVINCE = "江西省";
    // TODO: 2021/12/25 市
    private final static String CITY = "南昌市";
    // TODO: 2021/12/25 县
    private final static String DISTRICT = "青云谱区";
    // TODO: 2021/12/25 详细地址
    private final static String ADDREDDINFO = "气象路58号江西信息应用职业技术学院";

    // TODO: 2021/12/25 精度
    private final static String LNG = "115.912317";
    // TODO: 2021/12/25 维度
    private final static String LAT = "28.593909";
    public static void qiandao(String id, MsgInfo listenter) {
        String signResult = new Student((id!=null&&id.length()>6)?id:ID,PROVINCE,CITY,DISTRICT,ADDREDDINFO,LNG,LAT).signIn();
        System.out.println(signResult);
        if(signResult.contains("\"操作成功")){
            System.out.println("签到成功");
            listenter.sendMsg("[ "+((id!=null&&id.length()>6)?id:ID)+"] 签到成功!");
        }else if(signResult.contains("今日已签到成功")){
            System.out.println("今日已签到!");
            listenter.sendMsg("[ "+((id!=null&&id.length()>6)?id:ID)+"] 今日已签到!");
        }else {
            System.out.println("签到失败！");
            listenter.sendMsg("[ "+((id!=null&&id.length()>6)?id:ID)+"] 签到失败!");
        }
    }
}
@Getter
@Setter
class Student{
    private String id;//学号
    private String province;//省名
    private String city;//城市名
    private String district;//区县名
    private String addressInfo;//区县名
    private String LNG;//经度
    private String LAT;//纬度
    public String signIn(){
        Map<String ,String> map = new HashMap<>();
        map.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        map.put("Cookie",getCookie());
        map.put("Referer","https,//fxgl.jx.edu.cn/4136012939/user/qdbp");
        map.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.49");
        String url = "https://fxgl.jx.edu.cn/4136012939/studentQd/saveStu";
        //System.out.println(map);
        try {
            String a = sendPost(url,getUrl(),getCookie());

            return a;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String getCookie(){

        String urltwo = "https://fxgl.jx.edu.cn/4136012939/public/homeQd?loginName="+getId()+"&loginType=0";
        try {
            String cookies = OkHttpUtils.getCookie(urltwo);
            return cookies;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    private String e(String str){
        try {
            return URLEncoder.encode(str,UTF_8);
        } catch (UnsupportedEncodingException e) {

        }
        return  null;
    }
//    public Student() {
//        this.id = "20196293";
//        this.province = "%E6%B1%9F%E8%A5%BF%E7%9C%81";
//        this.city = "%E5%8D%97%E6%98%8C%E5%B8%82";
//        this.district = "%E9%9D%92%E4%BA%91%E8%B0%B1%E5%8C%BA";
//        this.addressInfo = "%E6%B0%94%E8%B1%A1%E8%B7%AF58%E5%8F%B7%E6%B1%9F%E8%A5%BF%E4%BF%A1%E6%81%AF%E5%BA%94%E7%94%A8%E8%81%8C%E4%B8%9A%E6%8A%80%E6%9C%AF%E5%AD%A6%E9%99%A2";
//        this.LNG = "115.912317";
//        this.LAT = "28.593909";
//    }

    public Student(String id, String province, String city, String district, String addressInfo, String LNG, String LAT) {
        this.id = id;
        this.province = e(province);
        this.city = e(city);
        this.district = e(district);
        this.addressInfo = e(addressInfo);
        this.LNG = LNG;
        this.LAT = LAT;
    }

    public String getUrl(){
        return "province="+province+
                "&city="+city+
                "&district="+district+
                "&street="+addressInfo+
                "&xszt=0&jkzk=0&jkzkxq=&sfgl=1&gldd=&mqtw=0&mqtwxq=&zddlwz="+province+city+district+"&sddlwz=&bprovince="+province+"&bcity="+city+"&bdistrict="+district+"&bstreet="+addressInfo+"&sprovince="+province+"&scity="+city+"&sdistrict="+district+"&lng="+LNG+"&lat="+LAT+"&sfby=1";

    }
    public static String sendPost(String url, String param,String cookie) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("cookie", cookie);
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }
}